﻿using DevTeam;
using System;
using System.Collections.Generic;
using System.Data;

namespace ConfExpert.Forms.ImportWizards.Yukleme
{
    public partial class TopBilgileri : XITopCekiListesi
    {
        int YuklemeId;
        public TopBilgileri(int YuklemeId) :
            base(YuklemeId)
        {
            InitializeComponent();
            this.YuklemeId = YuklemeId;
            this.FileDone += TopBilgileri_FileDone;
            btnSave.Enabled = false;

            splitter.AutoSize();
        }

        void gvMaster_FocusedRowChanged(object sender, DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventArgs e)
        {
            gvDetail.BestFitColumns();
        }

        void TopBilgileri_FileDone(DataSet ds, string FileName)
        {
            foreach (DataTable dt in ds.Tables)
            {
                int basliksatiri = 0;
                Dictionary<string, int> indexes = new Dictionary<string, int>();
                for (int colIndex = 0; colIndex < dt.Columns.Count; colIndex++)
                {
                    DataRow dr0 = dt.Rows[basliksatiri];
                    switch (dr0[colIndex].ToString())
                    {
                        case "Article": indexes.Add("Article", colIndex); break;
                        case "LotNo": indexes.Add("LotNo", colIndex); break;
                        case "RollNo": indexes.Add("VendorRollId", colIndex); break;
                        case "Qty": indexes.Add("Qty", colIndex); break;
                        case "WidthUsable": indexes.Add("WidthUsable", colIndex); break;
                        case "WidthActual": indexes.Add("WidthActual", colIndex); break;
                    }
                }

                for (int i = basliksatiri + 1; i < dt.Rows.Count; i++)
                {
                    DataRow dr = dt.Rows[i];

                    if (dr[indexes["Qty"]] != DBNull.Value)
                    {
                        DataRow drRoll = Roll.NewRow();
                        string article = dr[indexes["Article"]].ToString();
                        if (Article.Select("Article='" + article + "'").Length == 0)
                        {
                            DataRow drArticle = Article.NewRow();
                            drArticle["Article"] = article;
                            Article.Rows.Add(drArticle);
                        }

                        drRoll["Article"] = article;
                        if (dr[indexes["LotNo"]] != DBNull.Value)
                            drRoll["LotNo"] = dr[indexes["LotNo"]];
                        if (dr[indexes["VendorRollId"]] != DBNull.Value)
                            drRoll["VendorRollId"] = dr[indexes["VendorRollId"]];
                        drRoll["Qty"] = dr[indexes["Qty"]];
                        if (dr[indexes["WidthUsable"]] != DBNull.Value)
                            drRoll["WidthUsable"] = dr[indexes["WidthUsable"]];
                        if (dr[indexes["WidthActual"]] != DBNull.Value)
                            drRoll["WidthActual"] = dr[indexes["WidthActual"]];

                        Roll.Rows.Add(drRoll);
                    }
                }                
            }

        }

    }
}
